﻿namespace HySoft.Cash.DB
{
    using Huayou.Common.Database;
    using Huayou.Utils;
    using HySoft.Cash.BaseClass;
    using HySoft.Cash.Structure;
    using System;
    using System.Data;

    public class TorginfoDB : BaseDB
    {
        public TorginfoDB(DBOperate oper) : base(oper)
        {
        }

        public bool Add(TorginfoDT detail)
        {
            int num = 0;
            string strSql = string.Concat(new object[] { 
                "INSERT INTO tOrgInfo (AgreementDate,OrgName,TradingName,ABN,Industry,OfficeAddress,WebSite,ContactPerson,sPosition,ExpCreditLimit,PostalAddress,ContactPhone1,ContactPhone2,Fax,Mobile,PreferredEmail,AltEmail1,AltEmail2,UserName,PassWord,RegDate,IsValid,CreditLimit,CurrentBalance,AvailableBalance) VALUES ('", StringUtils.ToSQL(detail.AgreementDate), "','", StringUtils.ToSQL(detail.OrgName), "','", StringUtils.ToSQL(detail.TradingName), "','", StringUtils.ToSQL(detail.ABN), "','", StringUtils.ToSQL(detail.Industry), "','", StringUtils.ToSQL(detail.OfficeAddress), "','", StringUtils.ToSQL(detail.WebSite), "','", StringUtils.ToSQL(detail.ContactPerson), 
                "','", StringUtils.ToSQL(detail.sPosition), "',", detail.ExpCreditLimit, ",'", StringUtils.ToSQL(detail.PostalAddress), "','", StringUtils.ToSQL(detail.ContactPhone1), "','", StringUtils.ToSQL(detail.ContactPhone2), "','", StringUtils.ToSQL(detail.Fax), "','", StringUtils.ToSQL(detail.Mobile), "','", StringUtils.ToSQL(detail.PreferredEmail), 
                "','", StringUtils.ToSQL(detail.AltEmail1), "','", StringUtils.ToSQL(detail.AltEmail2), "','", StringUtils.ToSQL(detail.UserName), "','", StringUtils.ToSQL(detail.PassWord), "','", StringUtils.ToSQL(detail.RegDate), "',", detail.IsValid, ",", detail.CreditLimit, ",", detail.CurrentBalance, 
                ",", detail.AvailableBalance, ")"
             });
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return (num == 1);
        }

        public int AddId(TorginfoDT detail)
        {
            int num = 0;
            int num2 = 0;
            string strSql = string.Concat(new object[] { 
                "INSERT INTO tOrgInfo (AgreementDate,OrgName,TradingName,ABN,Industry,OfficeAddress,WebSite,ContactPerson,sPosition,ExpCreditLimit,PostalAddress,ContactPhone1,ContactPhone2,Fax,Mobile,PreferredEmail,AltEmail1,AltEmail2,UserName,PassWord,RegDate,IsValid,CreditLimit,CurrentBalance,AvailableBalance) VALUES ('", StringUtils.ToSQL(detail.AgreementDate), "','", StringUtils.ToSQL(detail.OrgName), "','", StringUtils.ToSQL(detail.TradingName), "','", StringUtils.ToSQL(detail.ABN), "','", StringUtils.ToSQL(detail.Industry), "','", StringUtils.ToSQL(detail.OfficeAddress), "','", StringUtils.ToSQL(detail.WebSite), "','", StringUtils.ToSQL(detail.ContactPerson), 
                "','", StringUtils.ToSQL(detail.sPosition), "',", detail.ExpCreditLimit, ",'", StringUtils.ToSQL(detail.PostalAddress), "','", StringUtils.ToSQL(detail.ContactPhone1), "','", StringUtils.ToSQL(detail.ContactPhone2), "','", StringUtils.ToSQL(detail.Fax), "','", StringUtils.ToSQL(detail.Mobile), "','", StringUtils.ToSQL(detail.PreferredEmail), 
                "','", StringUtils.ToSQL(detail.AltEmail1), "','", StringUtils.ToSQL(detail.AltEmail2), "','", StringUtils.ToSQL(detail.UserName), "','", StringUtils.ToSQL(detail.PassWord), "','", StringUtils.ToSQL(detail.RegDate), "',", detail.IsValid, ",", detail.CreditLimit, ",", detail.CurrentBalance, 
                ",", detail.AvailableBalance, ")"
             });
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                    if (num > 0)
                    {
                        strSql = "SELECT @@IDENTITY AS 'Identity'";
                        DataTable table = base.ExecuteForDataTable(strSql);
                        if (table.Rows.Count > 0)
                        {
                            num2 = Convert.ToInt32(table.Rows[0][0].ToString());
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            if (num == 1)
            {
                return num2;
            }
            return 0;
        }

        public bool Delete(int Sid)
        {
            int num = 0;
            string strSql = "DELETE FROM tOrgInfo where (Sid=" + Sid + ")";
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return (num == 1);
        }

        public bool Edit(TorginfoDT detail)
        {
            int num = 0;
            string strSql = string.Concat(new object[] { 
                "UPDATE tOrgInfo set AgreementDate='", StringUtils.ToSQL(detail.AgreementDate), "',OrgName='", StringUtils.ToSQL(detail.OrgName), "',TradingName='", StringUtils.ToSQL(detail.TradingName), "',ABN='", StringUtils.ToSQL(detail.ABN), "',Industry='", StringUtils.ToSQL(detail.Industry), "',OfficeAddress='", StringUtils.ToSQL(detail.OfficeAddress), "',WebSite='", StringUtils.ToSQL(detail.WebSite), "',ContactPerson='", StringUtils.ToSQL(detail.ContactPerson), 
                "',sPosition='", StringUtils.ToSQL(detail.sPosition), "',ExpCreditLimit=", detail.ExpCreditLimit, ",PostalAddress='", StringUtils.ToSQL(detail.PostalAddress), "',ContactPhone1='", StringUtils.ToSQL(detail.ContactPhone1), "',ContactPhone2='", StringUtils.ToSQL(detail.ContactPhone2), "',Fax='", StringUtils.ToSQL(detail.Fax), "',Mobile='", StringUtils.ToSQL(detail.Mobile), "',PreferredEmail='", StringUtils.ToSQL(detail.PreferredEmail), 
                "',AltEmail1='", StringUtils.ToSQL(detail.AltEmail1), "',AltEmail2='", StringUtils.ToSQL(detail.AltEmail2), "',UserName='", StringUtils.ToSQL(detail.UserName), "',PassWord='", StringUtils.ToSQL(detail.PassWord), "',RegDate='", StringUtils.ToSQL(detail.RegDate), "',IsValid=", detail.IsValid, ",CreditLimit=", detail.CreditLimit, ",CurrentBalance=", detail.CurrentBalance, 
                ",AvailableBalance=", detail.AvailableBalance, " where (Sid=", detail.Sid, ")"
             });
            try
            {
                try
                {
                    num = base.ExecuteForInt(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return (num == 1);
        }

        public TorginfoDT Get(int Sid)
        {
            TorginfoDT odt = new TorginfoDT();
            try
            {
                try
                {
                    string strSql = "select * from tOrgInfo where (Sid=" + Sid + ")";
                    DataTable table = base.ExecuteForDataTable(strSql);
                    DataRow row = null;
                    if (table.Rows.Count >= 0)
                    {
                        row = table.Rows[0];
                        odt.AgreementDate = Convert.ToString(row["AgreementDate"]);
                        odt.OrgName = Convert.ToString(row["OrgName"]);
                        odt.TradingName = Convert.ToString(row["TradingName"]);
                        odt.ABN = Convert.ToString(row["ABN"]);
                        odt.Industry = Convert.ToString(row["Industry"]);
                        odt.OfficeAddress = Convert.ToString(row["OfficeAddress"]);
                        odt.WebSite = Convert.ToString(row["WebSite"]);
                        odt.ContactPerson = Convert.ToString(row["ContactPerson"]);
                        odt.sPosition = Convert.ToString(row["sPosition"]);
                        odt.ExpCreditLimit = Convert.ToDecimal(row["ExpCreditLimit"]);
                        odt.PostalAddress = Convert.ToString(row["PostalAddress"]);
                        odt.ContactPhone1 = Convert.ToString(row["ContactPhone1"]);
                        odt.ContactPhone2 = Convert.ToString(row["ContactPhone2"]);
                        odt.Fax = Convert.ToString(row["Fax"]);
                        odt.Mobile = Convert.ToString(row["Mobile"]);
                        odt.PreferredEmail = Convert.ToString(row["PreferredEmail"]);
                        odt.AltEmail1 = Convert.ToString(row["AltEmail1"]);
                        odt.AltEmail2 = Convert.ToString(row["AltEmail2"]);
                        odt.UserName = Convert.ToString(row["UserName"]);
                        odt.PassWord = Convert.ToString(row["PassWord"]);
                        odt.RegDate = Convert.ToString(row["RegDate"]);
                        odt.IsValid = Convert.ToInt32(row["IsValid"]);
                        odt.CreditLimit = Convert.ToDecimal(row["CreditLimit"]);
                        odt.CurrentBalance = Convert.ToDecimal(row["CurrentBalance"]);
                        odt.AvailableBalance = Convert.ToDecimal(row["AvailableBalance"]);
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return odt;
        }

        public DataTable GetList()
        {
            DataTable table = null;
            try
            {
                try
                {
                    string strSql = "select * from tOrgInfo";
                    table = base.ExecuteForDataTable(strSql);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return table;
        }
    }
}

